package org.nch.videoLauncher.presenter;

import org.nch.videoLauncher.view.VideoDetailsView;
import org.nch.videoLauncher.view.IVideoDetailsView;
import org.nch.videoLauncher.domain.VideoItem;
import org.nch.videoLauncher.domain.PlaybackEvent;
import org.nch.videoLauncher.task.VideoItemTask;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Jan 3, 2009
 * Time: 4:57:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class VideoDetailsPresenter {

    private IVideoDetailsView view;
    private VideoItem videoItem;
    private VideoItemTask task;
    private List<PlaybackEvent> playbackEvents;

    public VideoDetailsPresenter(IVideoDetailsView iview, VideoItem vi){

        this.view = iview;
        this.videoItem = vi;
        task = new VideoItemTask();

        if (videoItem.getSportsEvent() != null && !(videoItem.getSportsEvent().getSport().equals("") || (videoItem.getSportsEvent().getSport() == null))){

            view.setSportsTitleText(videoItem.getTitle());
            view.setSportsKeywords(videoItem.getKeywords());
            view.setSportsDescriptionText(videoItem.getDescription());
            view.setSportsSport(videoItem.getSportsEvent().getSport());
            view.setSportsTeam(videoItem.getSportsEvent().getTeam());
            view.setSportsSeason(videoItem.getSportsEvent().getSeason());
            if (videoItem.getUrl() != null && !videoItem.getUrl().equals("")){
                view.setSportsOpenUrlEnabled(true);
            }
            view.setSportsTabEnabled(true);
            view.showSportsTab();

        } else if (videoItem.getTvInfo() != null && !(videoItem.getTvInfo().getShow().equals("") || (videoItem.getTvInfo().getShow() == null))){

            view.setTvTitleText(videoItem.getTitle());
            view.setTvGenreText(videoItem.getKeywords());
            view.setTvShow(videoItem.getTvInfo().getShow());
            if (videoItem.getImdbInfo() != null){
                if (videoItem.getImdbInfo().getCover() != null){
                    view.setTvCover(videoItem.getImdbInfo().getCover());
                }
            }
            view.setTvRatingText(videoItem.getImdbInfo().getRating());
            view.setTvDescriptionText(videoItem.getDescription());

            if (String.valueOf(videoItem.getTvInfo().getSeason()) == "null")
                view.setTvSeason("");
            else
                view.setTvSeason(String.valueOf(videoItem.getTvInfo().getSeason()));

            if (String.valueOf(videoItem.getTvInfo().getEpisode()) == "null")
                view.setTvEpisode("");
            else
                view.setTvEpisode(String.valueOf(videoItem.getTvInfo().getEpisode()));            

            if (videoItem.getUrl() != null && !videoItem.getUrl().equals("")){
                view.setTvOpenUrlEnabled(true);
            }
            view.setTvTabEnabled(true);
            view.showTvTab();

        } else {

            view.setMovieTitleText(videoItem.getTitle());
            view.setMovieGenreText(videoItem.getKeywords());
            if (videoItem.getImdbInfo() != null){
                if (videoItem.getImdbInfo().getCover() != null){
                    view.setMovieCover(videoItem.getImdbInfo().getCover());
                }
            }
            view.setMovieDirectedByText(videoItem.getImdbInfo().getDirectedBy());
            view.setMovieRatingText(videoItem.getImdbInfo().getRating());
            view.setMovieYearText(videoItem.getImdbInfo().getDate());
            view.setMovieDescriptionText(videoItem.getDescription());
            view.setMovieRatingText(videoItem.getImdbInfo().getRating());
            view.setMovieMpaaText(videoItem.getImdbInfo().getMpaa());

            view.setMovieRuntime(videoItem.getRunTime());

            if (videoItem.getUrl() != null && !videoItem.getUrl().equals("")){
                view.setMovieOpenUrlEnabled(true);
            }
            view.setMovieTabEnabled(true);
            view.showMovieTab();
        }
    }


    public void playRequested(){
        task.playVideoItem(videoItem);
    }

    public void openUrlRequested(){
        task.openUrl(videoItem);
    }

    public void showHistory(){

        if (playbackEvents == null)
            playbackEvents = task.getHistoryForVideoItem(videoItem);

        view.setHistoryList(playbackEvents);

    }

}
